package net.lainiao.mycost.utils;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.repository.ProcessDefinition;

import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2017/3/21.
 */
public class ProcessUtils {
    //流程定义的Key
    public static final String PROCESS_DEFINITON_EKY = "ExpenseProcess";
    //流程变量名称
    public static final String VARIABLE_NAME = "expa";
    public static final String PROCESS_ASSIGNEE_USER = "user";
    public static final String PROCESS_ASSIGNEE_MANAGER = "manager";
    public static final String PROCESS_ASSIGNEE_FINANCE = "finance";

    public static ProcessEngine getProcessEngine() {
        return ProcessEngines.getDefaultProcessEngine();
    }

    public static ActivityImpl getProcessMap(String processdefId, String executionId) {
        //仓库服务
        RepositoryService repositoryService = getProcessEngine().getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processdefId).singleResult();
        System.out.println("查看流程定义名称:" + processDefinition.getName());

        ProcessDefinitionImpl pdImpl = (ProcessDefinitionImpl) processDefinition;
        String processDefinitionId = pdImpl.getId();
        System.out.println("刘成表示:" + processDefinitionId);

        ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(processDefinitionId);
        ActivityImpl actimpl = null;

        RuntimeService runtimeService = getProcessEngine().getRuntimeService();
        ExecutionEntity executionEntity = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(executionId).singleResult();

        //执行实例
        String activityId = executionEntity.getActivityId();
        System.out.println("当前执行节点:" + activityId);

        //获取当前任务所有节点
        List<ActivityImpl> list = def.getActivities();

        for (ActivityImpl impl : list) {
            String id = impl.getId();
            if (id.equals(activityId)) {
                actimpl = impl;
                break;
            }
        }
        return actimpl;
    }

    public static InputStream findProcessImg(String processDefId) {
        RepositoryService repositoryService = getProcessEngine().getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefId).singleResult();
        String name = processDefinition.getDiagramResourceName();
        InputStream is = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), name);
        return is;
    }
}
